<template>
  <div class="demo">
    <div class="demo-title">完全控制的上传列表</div>
    <div class="demo-content">
      <Upload
        action="https://www.mocky.io/v2/5cc8019d300000980a055e76"
        :multiple="true"
        :file-list="fileList"
        @change="handleChange"
      >
        <Button><UploadOutlined />Upload</Button>
      </Upload>
    </div>
  </div>
</template>

<script lang="ts" setup>
  import { ref } from 'vue';
  import { UploadOutlined } from '@ant-design/icons-vue';
  import Button from '@sscd/button';
  import Upload from '@sscd/upload';
  import type { UploadChangeParam, UploadProps } from '@sscd/upload';
  const fileList = ref<UploadProps['fileList']>([
    {
      uid: '-1',
      name: 'xxx.png',
      status: 'done',
      url: 'http://www.baidu.com/xxx.png',
    },
  ]);
  const handleChange = (info: UploadChangeParam) => {
    let resFileList = [...info.fileList];
    // 1. Limit the number of uploaded files
    //    Only to show two recent uploaded files, and old ones will be replaced by the new
    resFileList = resFileList.slice(-2);
    // 2. read from response and show file link
    resFileList = resFileList.map((file) => {
      if (file.response) {
        // Component will show file.url as link
        file.url = file.response.url;
      }
      return file;
    });
    fileList.value = resFileList;
  };
</script>
